package hos.base.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;

/**
 * <p>Title: DialogUtils </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-05-12 9:21
 */
public class DialogUtils {


    /**
     * Value of dp to value of px.
     *
     * @param dpValue The value of dp.
     * @return value of px
     */
    public static int dp2px(final float dpValue) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static GradientDrawable getGradientDrawable(int radius) {
        return getGradientDrawable(Color.WHITE, radius);
    }

    public static GradientDrawable getGradientDrawable() {
        return getGradientDrawable(Color.WHITE, dp2px(4));
    }

    public static GradientDrawable getGradientDrawable(int color, int radius) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(color);
        gradientDrawable.setCornerRadius(radius);
        return gradientDrawable;
    }

    public static GradientDrawable getGradientDrawableBottom(int radius) {
        return getGradientDrawableBottom(Color.WHITE, radius);
    }

    public static GradientDrawable getGradientDrawableBottom() {
        return getGradientDrawableBottom(Color.WHITE, dp2px(4));
    }

    public static GradientDrawable getGradientDrawableBottom(int color, int radius) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(color);
        DialogUtils.setCornerRadii(gradientDrawable, radius, radius, 0, 0);
        return gradientDrawable;
    }

    static void setCornerRadii(GradientDrawable drawable,
                               float topLeft, float topRight, float bottomLeft, float bottomRight) {
        drawable.setCornerRadii(new float[]{topLeft, topLeft, topRight, topRight,
                bottomLeft, bottomLeft, bottomRight, bottomRight});
    }

    public static int getWindowWidth(@NonNull Dialog dialog) {
        Window window = dialog.getWindow();
        if (window == null) {
            return -1;
        }
        return getWindowWidth(window);
    }

    public static int getWindowWidth(@NonNull Activity activity) {
        Window window = activity.getWindow();
        if (window == null) {
            return -1;
        }
        return getWindowWidth(window);
    }

    public static int getWindowWidth(@NonNull Window window) {
        return getWindowWidth(window.getWindowManager());
    }

    public static int getWindowWidth(@NonNull WindowManager windowManager) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            return windowManager.getCurrentWindowMetrics().getBounds().width();
        }
        Display defaultDisplay = windowManager.getDefaultDisplay();
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            defaultDisplay.getRealSize(point);
        } else {
            defaultDisplay.getSize(point);
        }
        return point.x;
    }
}
